<?php


declare (strict_types=1);

namespace app\admin\model\store;

use app\common\model\store\User as StoreUserModel;
use cores\exception\BaseException;
use think\Exception;

/**
 * 机构用户模型
 * Class StoreUser
 * @package app\admin\model
 */
class User extends StoreUserModel
{
    public function getAllList()
    {
        return $this->where(['is_delete' => 0])->select();
    }

    public function getTreeList($parent_store_user_id = 0)
    {
        $list = $this->where(['is_delete' => 0, 'parent_store_user_id' => $parent_store_user_id])->select();
        foreach ($list as $item) {
            $item->children = $this->getTreeList($item->store_user_id);
        }
        return $list;
    }

    /**
     * 删除小程序下的机构用户
     * @param $storeId
     * @return false|int
     */
    public static function setDelete(int $storeId)
    {
        static::update(['is_delete' => '1'], ['store_id' => $storeId]);
        return true;
    }

    /**
     * 机构用户登录
     * @param int $storeId
     * @throws BaseException
     * @throws Exception
     */
    public function login(int $storeId)
    {
        // 获取该商户管理员用户信息
        $userInfo = $this->getSuperStoreUser($storeId);
        if (empty($userInfo)) {
            throwError('超级管理员用户信息不存在');
        }
    }

    /**
     * 获取该商户管理员用户信息
     * @param int $storeId
     * @return static|null
     */
    private function getSuperStoreUser(int $storeId)
    {
        return static::detail(['store_id' => $storeId, 'is_super' => 1], ['wxapp']);
    }
}
